const util = require('../../utils/util.js'); //路径根据自己的文件目录来
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    color: '#FFFFFF',
    background: '#FFFFFF',
    show: true,
    animated: false,
    back: false,
    title: "骑手入驻",
    
    mobile: '',//手机号码
    realName: '',//真实姓名
    idcard: '',//身份证号码
    captcha: '',//手机验证码
    rider_type: '1',//申请骑手类型
    prefecture: '',//所在区域
    /* 骑手类型选择*/
    radioItems: [
      {name: '司机', value: '0', checked: false},
      {name: '代理商', value: '1', checked: false},
      {name: '业务员', value: '2', checked: false}
    ],
    region: ['请选择', ''],
    valueRegion: [0, 0],
    cityId:0,
    district:[],
    multiArray:[],
    multiIndex: [0, 0],
  },
  bindCategoryChange: function(e){
		var value = e.detail.value;
    console.log(value);
		this.setData({
			rider_type: value
		})
	},
  bindRegionChange: function (e) {
    var value = e.detail.value;
    console.log(value);
    this.setData({
  		prefecture: [value[0]]+[value[1]]+[value[2]],
      region: e.detail.value,
      // cityId: city.v,
      valueRegion: [0,0,0],
      prefecture_text: [value[0]]+","+[value[1]]+","+[value[2]],
    });
    console.log(this.data.prefecture_text);
    this.initialize();
  },
  bindMultiPickerColumnChange:function(e){
    let that = this, column = e.detail.column, value = e.detail.value, currentCity = this.data.district[value] || { c: [] }, multiArray = that.data.multiArray, multiIndex = that.data.multiIndex;
    multiIndex[column] = value;
    switch (column){
      case 0:
        let areaList = currentCity.c[0] || { c: [] };
        multiArray[1] = currentCity.c.map((item)=>{
          return item.n;
        });
        // multiArray[2] = areaList.c.map((item)=>{
        //   return item.n;
        // });
      break;
      case 1:
        let cityList = that.data.district[multiIndex[0]].c[multiIndex[1]].c || [];
        // multiArray[2] = cityList.map((item)=>{
        //   return item.n;
        // });
      break;
      case 2:
      
      break;
    }
    this.setData({ multiArray: multiArray, multiIndex: multiIndex});
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取当前小程序的页面栈
    var that = this;
    if (app.globalData.userInfo) {
      if(!app.globalData.userInfo.rider_apply){
        
      }else{
        if(app.globalData.userInfo.rider_apply.status == 0){
          wx.showModal({
            title: '',
            content: '您的账号正在审核中\r\n请您耐心等待',
            showCancel: false,
            confirmText: "确定",
            cancelText: "取消",
          	cancelColor: "#5E6266",
          	confirmColor: "#02B3BA",
            success: function (res) {
              if (res.confirm) {
                /* 跳转到申请骑手*/
          			wx.reLaunch({
          				url: '/page/index/index'
          			});
              } else {
          			return false; 
              }
            }
          });
          return false;
        }
        wx.setStorageSync("mobile", app.globalData.userInfo.rider_apply.apply_mobile);
        wx.setStorageSync("realName", app.globalData.userInfo.rider_apply.apply_name);
        wx.setStorageSync("idcard", app.globalData.userInfo.rider_apply.IDcard);
      }
    }else{
      setTimeout(function () {
        if (app.globalData.userInfo) {
          if(!app.globalData.userInfo.rider_apply){
            if(app.globalData.userInfo.rider_apply.status == 0){
              wx.showModal({
                title: '',
                content: '您的账号正在审核中\r\n请您耐心等待',
                showCancel: false,
                confirmText: "确定",
                cancelText: "取消",
              	cancelColor: "#5E6266",
              	confirmColor: "#02B3BA",
                success: function (res) {
                  if (res.confirm) {
                    /* 跳转到申请骑手*/
              			wx.reLaunch({
              				url: '/page/index/index'
              			});
                  } else {
              			return false; 
                  }
                }
              });
            }
            return false;
          }else{
            wx.setStorageSync("mobile", app.globalData.userInfo.rider_apply.apply_mobile);
            wx.setStorageSync("realName", app.globalData.userInfo.rider_apply.apply_name);
            wx.setStorageSync("idcard", app.globalData.userInfo.rider_apply.IDcard);
          }
        }else{
          app.check(function(ret){}, '/page/rider_apply/step1');
        }
      }, 500)
    }
    //使用缓存保存基本信息
    var mobile = wx.getStorageSync("mobile");
    var realName = wx.getStorageSync("realName");
    var idcard = wx.getStorageSync("idcard");
    var rider_type = wx.getStorageSync("rider_type");
    var prefecture = wx.getStorageSync("prefecture");
    if(mobile){
      that.setData({
        mobile: mobile
      })
    }
    if(realName){
      that.setData({
        realName: realName
      })
    }
    if(idcard){
      that.setData({
        idcard: idcard
      })
    }
    app.request('/addons/cms/wxapp.area/index', {}, function (data, ret) {//获取地址信息
      that.setData({
        district: data
      });
    	that.initialize();
    }, function (data, ret) {
      app.error(ret.msg);
    });
  },
  initialize:function(){
    let that = this, province = [], city = [], area = [];
    if (that.data.district.length) {
      let cityChildren = that.data.district[0].c || [];
      let areaChildren = cityChildren.length ? (cityChildren[0].c || []) : [];
      that.data.district.forEach(function (item) {
        province.push(item.n);
      });
      cityChildren.forEach(function (item) {
        city.push(item.n);
      });
      areaChildren.forEach(function (item) {
        area.push(item.n);
      });
      that.setData({
        multiArray: [province, city]
      });
    }
  },
  /* 监听真实姓名改变*/
  realNameChange: function(e){
  	console.log(e);
  	var value = e.detail.value;
  	this.setData({
  		realName: value
  	})
  },
  mobileChange: function(e){
  	console.log(e);
  	var value = e.detail.value;
  	this.setData({
  		mobile: value
  	})
  },
  idcardChange: function(e){
  	console.log(e);
  	var value = e.detail.value;
  	this.setData({
  		idcard: value
  	})
  },
  captchaChange: function(e){
  	console.log(e);
  	var value = e.detail.value;
  	this.setData({
  		captcha: value
  	})
  },
	/* 骑手申请下一步 */
	nextStep: function(){
    var that = this;
		if(that.data.mobile=='') {
			app.error('请输入手机号');
			return false;
		}else if(that.data.mobile.length != 11){
      app.error('手机号格式错误');
      return false;
    }
		if(that.data.realName=='') {
			app.error('请输入真实姓名');
			return false;
		}
		if(that.data.idcard=='') {
			app.error('请输入身份证号码');
			return false;
		}
    if (!util.checkIdCard(this.data.idcard)){
      app.error('身份证号码格式错误');
      return false;
    }
		if(that.data.captcha=='') {
			app.error('请输入验证码');
			return false;
		}
    if(that.data.rider_type=='') {
    	app.error('请选择骑手类型');
    	return false;
    }
    if(that.data.prefecture=='') {
    	app.error('请选择所在区域');
    	return false;
    }
    /* 验证码验证*/
    app.request('/api/sms/check', {mobile: that.data.mobile, captcha: that.data.captcha}, function (data, ret) {//获取地址信息
      if(ret.code==1){
        //使用缓存保存基本信息
        wx.setStorageSync("mobile", that.data.mobile);
        wx.setStorageSync("realName", that.data.realName);
        wx.setStorageSync("idcard", that.data.idcard);
        wx.setStorageSync("rider_type", that.data.rider_type);
        wx.setStorageSync("prefecture", that.data.prefecture);
        wx.setStorageSync("prefecture_text", that.data.prefecture_text);
        wx.navigateTo({
          url: "/page/rider_apply/step2?mobile="+that.data.mobile+"&realName="+that.data.realName+"&idcard="+that.data.idcard+"&rider_type="+that.data.rider_type+"&prefecture="+that.data.prefecture+"&prefecture_text="+that.data.prefecture_text
        })
      }else{
        app.error(ret.msg);
        return false;
      }
    }, function (data, ret) {
      app.error(ret.msg);
      return false;
    });
	},
  sendSms: function(){
    var that = this;
    if(that.data.mobile=='') {
    	app.error('请输入手机号');
    	return false;
    }else if(that.data.mobile.length != 11){
      app.error('手机号格式错误');
      return false;
    }
    app.request('/api/sms/send', {mobile: that.data.mobile}, function (data, ret) {//获取地址信息
      app.error(ret.msg);
    }, function (data, ret) {
      app.error(ret.msg);
    });
  }
})